#!/bin/bash
echo "设置支持中文"
sudo chmod 777 /etc/locate.conf
cat>/etc/locate.conf<<EOF
LANG="zh_CN.UTF-8"
SYSFONT="latarcyrheb-sun16"
SUPPORTED="zh_CN.UTF-8:zh_CN:zh"
EOF

sudo yum install -y unzip wget
echo "设置开机不启动防火墙，以及开启启动docker"
sudo systemctl enable docker
sudo systemctl disable firewalld

echo "关闭防火墙，启动docker..."
sudo systemctl stop firewalld
sudo systemctl start docker

docker rm -f $(docker ps -aq)
sudo rm -rf /mydata && sudo mkdir -p /mydata/mysql/conf && sudo chmod 777 -R /mydata

echo "自定义mysql的配置文件..."
cat>/mydata/mysql/conf/my.cnf<<EOF
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
EOF
sudo chmod 777 -R /mydata
cp -r /home/vagrant/share/sql/ /mydata/mysql/sql
docker network create --driver bridge nacos-net

echo "启动mysql..."
docker run -d -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-v /mydata/mysql/sql:/sql \
-e MYSQL_ROOT_PASSWORD=root \
--restart=always \
--network nacos-net \
mysql:5.7

sudo chmod 777 -R /mydata


echo "执行sql脚本创建数据库..."
sleep 15
docker exec  mysql mysql -uroot -proot -e "source /sql/gulimall_admin.sql"
docker exec  mysql mysql -uroot -proot -e "source /sql/gulimall_oms.sql"
docker exec  mysql mysql -uroot -proot -e "source /sql/gulimall_pms.sql"
docker exec  mysql mysql -uroot -proot -e "source /sql/gulimall_sms.sql"
docker exec  mysql mysql -uroot -proot -e "source /sql/gulimall_wms.sql"
docker exec  mysql mysql -uroot -proot -e "source /sql/gulimall_ums.sql"
docker exec  mysql mysql -uroot -proot -e "source /sql/nacos_config.sql"


docker run -d --name nacos01 nacos/nacos-server:1.1.4

sudo mkdir -p /mydata/nacos/conf  && sudo chmod 777 -R /mydata && sudo docker cp nacos01:/home/nacos/conf/ /mydata/nacos/conf && sudo mv /mydata/nacos/conf/conf/* /mydata/nacos/conf/ && sudo rm -rf /mydata/nacos/conf/conf/ && sudo docker cp nacos01:/home/nacos/logs /mydata/nacos/ && sudo mv /mydata/nacos/conf/application.properties /mydata/nacos/conf/application.properties.back && sudo chmod 777 -R /mydata
docker rm -f nacos01

echo "编辑nacos配置文件设置将数据持久化到mysql中..."
cat>/mydata/nacos/conf/application.properties<<EOF
spring.datasource.platform=mysql

db.num=1
db.url.0=jdbc:mysql://mysql:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=root
EOF

echo "启动nacos..."
docker run --name nacos01 -d \
-p 8848:8848 \
--privileged=true \
--restart=always \
-e JVM_XMS=512m \
-e JVM_XMX=512m \
-e MODE=standalone \
-e PREFER_HOST_MODE=hostname \
-v /mydata/nacos/conf:/home/nacos/conf \
--network nacos-net \
nacos/nacos-server:1.1.4

sudo chmod 777 -R /mydata



echo "启动redis..."
mkdir -p /mydata/redis/conf && touch /mydata/redis/conf/redis.conf && echo "appendonly yes"  >> /mydata/redis/conf/redis.conf

docker run -p 6379:6379 --name redis \
-v /mydata/redis/data:/data \
-v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf \
--restart=always \
-d redis redis-server /etc/redis/redis.conf


echo "启动ElasticSearch..."
mkdir -p /mydata/elasticsearch/config && echo "http.host: 0.0.0.0" >/mydata/elasticsearch/config/elasticsearch.yml && docker network create --driver bridge es-network
mkdir -p /mydata/elasticsearch/plugins/ik && cp /home/vagrant/share/elasticsearch-analysis-ik-7.6.2.zip /mydata/elasticsearch/plugins/ik/ && cd /mydata/elasticsearch/plugins/ik/ && unzip elasticsearch-analysis-ik-7.6.2.zip && rm -rf rm -rf elasticsearch-analysis-ik-7.6.2.zip

echo "修改ik分词库，添加我们自己的分词"
cat>/mydata/elasticsearch/plugins/ik/config/IKAnalyzer.cfg.xml<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
        <comment>IK Analyzer 扩展配置</comment>
        <!--用户可以在这里配置自己的扩展字典 -->
        <entry key="ext_dict"></entry>
         <!--用户可以在这里配置自己的扩展停止词字典-->
        <entry key="ext_stopwords"></entry>
        <!--用户可以在这里配置远程扩展字典 -->
        <entry key="remote_ext_dict">http://nginx/es/fenci.txt</entry>
        <!--用户可以在这里配置远程扩展停止词字典-->
        <!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
EOF

# 设置ik分词器
docker run -d -p 9200:9200 -p 9300:9300 \
-e  "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms64m -Xmx512m" \
-v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
-v  /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
--restart always \
--network es-network \
--name elasticsearch \
elasticsearch:7.6.2

sudo chmod -R 777 /mydata


echo "启动kibana..."
docker run -d \
-p 5601:5601 \
-e ELASTICSEARCH_HOSTS=http://elasticsearch:9200 \
--restart always \
--name kibana \
--network es-network \
kibana:7.6.2



echo "启动nginx..."
docker run -d -P --name nginx nginx:1.10
mkdir -p /mydata/nginx && docker cp nginx:/etc/nginx /mydata/nginx && mv /mydata/nginx/nginx /mydata/nginx/conf
mkdir -p /mydata/nginx/html && docker cp nginx:/usr/share/nginx/html /mydata/nginx
mkdir -p /mydata/nginx/html/es
cat>/mydata/nginx/html/es/fenci.txt<<EOF
尚硅谷
乔碧萝
EOF



docker rm -f nginx

docker run -d -p 80:80 \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/logs:/var/log/nginx \
-v /mydata/nginx/conf:/etc/nginx \
--network es-network \
--restart always \
--name nginx \
nginx:1.10

sudo chmod -R 777 /mydata/



